summaryrefslogtreecommitdiff
path: root/src/pages/[locale]/news/index.astro
blob: 3e1b994a82745db7a757f76aef1029469aefd995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---
import translate from '$i18n/translate';
import tPage from '$i18n/translations/pages/news';
import Page from '$layouts/Page.astro';
import getLocaleFromPath from '$lib/getLocaleFromPath';
import localeStaticPaths from '$lib/localeStaticPaths';
import { getCollection } from 'astro:content';
import relativePath from '$lib/relativePath';

const allNews = await getCollection('news');
const locale = getLocaleFromPath(Astro.url.pathname);
const allNewsInLocale = allNews.filter(isInLocale);
const t = translate(locale);

function isInLocale(newsItem: (typeof allNews)[number]) {
  const srcPath = newsItem.id;
  const newsItemLocale = srcPath.slice(0, srcPath.indexOf('/'));
  return newsItemLocale === locale;
}

export async function getStaticPaths() {
  return localeStaticPaths;
}
---

<Page title={t(tPage, { key: 'title' })}>
  <section>
    <h1>{t(tPage, { key: 'title' })}</h1>
    <div class="news-index">
      {
        allNewsInLocale.length > 0 ? (
          allNewsInLocale.map((item) => (
            <a
              class="news-index__item"
              href={relativePath(Astro.url.pathname, item.slug.replace('/', '-'))}
            >
              <h2>{item.data.title}</h2>
              <p>{item.data.summary}</p>
              <p class="link">{t(tPage, { key: 'read-article' })}</p>
            </a>
          ))
        ) : (
          <p>{t(tPage, { key: 'no-news' })}</p>
        )
      }
    </div>
  </section>
</Page>